package com.sun.java8;

import com.alibaba.fastjson.JSON;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;
import org.springframework.util.StringUtils;

import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

public class TestTime01 {
    static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    static DateTimeFormatter stf = DateTimeFormatter.ofPattern("yyyy.MM.dd HH:mm");
    static DateTimeFormatter stf2 = DateTimeFormatter.ofPattern("hh:mm:ss");
    static DateTimeFormatter stf3 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    static SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy年MM月dd日");

    @Test
    public void test01() {
       /* LocalDateTime parse = LocalDateTime.parse("2021-02-02 01:01:01", stf3);
        LocalDateTime parse2 = LocalDateTime.parse("2021-02-03 03:01:02", stf3);
        System.out.println(parse);
        Duration between = Duration.between(parse, parse2);
        long l = between.getSeconds();
        System.out.println(l);
        String s = LocalDate.now().toString();
        System.out.println(s);*/
        String time = "3661";
        int miao = Integer.valueOf(time) % 60;
        int fen = (Integer.valueOf(time) % 3600) / 60;
        int xiaoshi = Integer.valueOf(time) / 3600;
        System.out.println(miao);
        System.out.println(fen);
        System.out.println(xiaoshi);

    }


    public static void main(String[] args) throws ParseException {
//        for (int i = 0; i < 10; i++) {
//            new Thread(() -> {
//                LocalDate parse = LocalDate.parse("20210101", stf);
//                System.out.println(parse);
//            }).start();
//        }
        LocalTime localTime = LocalTime.of(23, 0, 0);
        System.out.println(localTime);
    }

    @Test
    public void test02() {
        LocalDate ld = LocalDate.now();
        System.out.println("LocalDate当前系统时间" + ld);//LocalDate当前系统时间2021-06-18
        LocalTime lt = LocalTime.now();
        System.out.println("LocalTime当前系统时间" + lt);//LocalTime当前系统时间15:30:42.341
        LocalDateTime ldt = LocalDateTime.now();
        System.out.println("LocalDateTime当前系统时间" + ldt);//LocalDateTime当前系统时间2021-06-18T15:30:42.341
    }

    @Test
    public void test03() {
        LocalDateTime ldt = LocalDateTime.of(2021, 01, 01, 01, 01, 01);

        System.out.println("LocalDateTime指定日期" + ldt);
        System.out.println("LocalDateTime加1年" + ldt.plusYears(1));
        System.out.println("LocalDateTime减1月" + ldt.minusMonths(1));
    }

    @Test
    public void test04() {
        //默认获取 UTC时区
        Instant instant = Instant.now();
        System.out.println(instant);

        OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);

        long l = instant.toEpochMilli();//毫秒
        System.out.println(l);
        Instant instant1 = Instant.ofEpochSecond(1);
        System.out.println(instant1);
    }

    //Duration  计算两个 时间之间的间隔
    //Period  计算两个日期间的间隔
    @Test
    public void test05() {
        Instant instant1 = Instant.now();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Instant instant2 = Instant.now();
        Duration duration = Duration.between(instant1, instant2);
        long l = duration.toMillis();
        System.out.println(l);

    }

    @Test
    public void test06() {
        LocalDate localDate = LocalDate.of(2020, 6, 1);
        LocalDate old = LocalDate.parse("2019-06-03");
        LocalDate now = LocalDate.now();
        Period between = Period.between(old, now);
        System.out.println(between.getYears());
        System.out.println(between.getMonths());
        System.out.println(between.getDays());

    }

    @Test
    public void test07() {
        LocalDate localDate = LocalDate.of(2021, 11, 19);
        LocalDate FRIDAY = localDate.with(TemporalAdjusters.next(DayOfWeek.FRIDAY));
        LocalDate MONDAY = localDate.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
        System.out.println(FRIDAY + "周五");
        System.out.println(MONDAY + "周一");
        //自定义
        LocalDate workDate = localDate.with((temporal) -> {
            LocalDate ld1 = (LocalDate) temporal;
            DayOfWeek week = ld1.getDayOfWeek();
            if (week.equals(DayOfWeek.FRIDAY)) {
                return ld1.plusDays(3);
            } else if (week.equals(DayOfWeek.SATURDAY)) {
                return ld1.plusDays(2);
            } else {
                return ld1.plusDays(1);
            }
        });
        System.out.println("下一个工作日" + workDate);
        LocalDate parse = LocalDate.parse("2021-06-19", stf);
        System.out.println(parse);
    }

    @Test
    public void test08() {
        String date = "2022-01-09";
        DateTimeFormatter stf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate parse = LocalDate.parse(date, stf);
        LocalDate plusDays = parse.plusDays(90);
        System.out.println(parse);
        System.out.println(plusDays);
        LocalDate now = LocalDate.now();
        System.out.println(now);
        long days = now.until(plusDays, ChronoUnit.DAYS);
        System.out.println(days);
        if (days<= 10 && days > 0){
            System.out.println("密码还有" + days + "天过期，请及时更换密码");
        }
    }

    @Test
    public void test09() throws ParseException {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String date = "2022-06-28";
        Date parse = format.parse(date);
        System.out.println(parse);
        calendar.setTime(parse);
        calendar.add(Calendar.DATE, 90);

        String format1 = format.format(calendar.getTime());
        System.out.println(format1);


        long passwordExpdateLong = calendar.getTime().getTime() - format.parse(format.format(new Date())).getTime();
        System.out.println(passwordExpdateLong);
        String str = String.valueOf(passwordExpdateLong / 24 / 60 / 60 / 1000);
        /*if ((Integer.valueOf(str) <= 10 && Integer.valueOf(str) > 0)) {
            resultParam.setMessage("密码还有" + passwordExpdateDay + "天过期，请及时更换密码");
        }*/
        System.out.println(str);

    }

    @Test
    public void test10() {
        File file = new File("C:\\Users\\sun\\Desktop\\新建文本文档 (2).txt");
        StringBuilder sb = new StringBuilder();
        try (FileReader fr = new FileReader(file)){

            int ch = 0;
            while ((ch = fr.read()) != -1){
                sb.append((char)ch);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        Map parse = (Map) JSON.parse(sb.toString());
        List<List> data = getLecturerList(parse, "01");
        System.out.println(data);
        if (null != data && !data.isEmpty()) {
            OutputStream os = null;
            try {
                String fileName =  "123.xlsx";
                os = new FileOutputStream("C:\\Users\\sun\\Desktop\\" + fileName);
                XSSFWorkbook xssfSheets = makeExcel(data);

                xssfSheets.write(os);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (null != os) {
                        os.close();
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Test
    public void test11(){
        Map map = new HashMap();
        map.put("msg","");
        map.put("flag","1");
        map.put("flag","1");
        List LecturerList = new ArrayList();
        Map map1 = new HashMap();
        List winningList = new ArrayList();
        map1.put("winningList",winningList);
        map1.put("userCode","0000056303");
        map1.put("number","1");
        map1.put("workDate","");
        map1.put("officePhone","");
        map1.put("undoubtedlyDate","");
        Map map2 = new HashMap();
        map2.put("code","31");
        map2.put("codeName","大专");
        map1.put("codeName",new HashMap());
        map1.put("departureDate","");
        map1.put("inspectionDate","");
        map1.put("moveDate","");
        map1.put("inspectionData","");
        map1.put("otherNote","");
        map1.put("moveNote","");
        map1.put("sex","1");
        map1.put("employDate","2010-03-25");
        map1.put("JobsState",null);
        map1.put("courseList",new ArrayList<>());
        map1.put("bornDate","2010-03-25");
        map1.put("userName","马丽艳");
        map1.put("technologyRank",null);
        map1.put("phone","18624000000");
        map1.put("workYear","0年");
        map1.put("userType","1");
        map1.put("comName","辽宁分公司本部直属营销服务部");
        map1.put("JobsName",null);
        map1.put("age","0");
        LecturerList.add(map1);
        map.put("LecturerList",LecturerList);
        List data = getLecturerList(map, "01");
        System.out.println(data);
        XSSFWorkbook workbook = makeExcel(data);
        String fileName =  "123.xls";
        try(OutputStream os = new FileOutputStream("C:\\Users\\sun\\Desktop\\" + fileName)){
            BufferedOutputStream bos = new BufferedOutputStream(os);
//            bos.flush();

            workbook.write(os);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public List<List> getLecturerList(Map<String, Object> selMap, String lecturerType) {
        List<List> lists = new ArrayList<>();
        List list = new ArrayList();

        List lecturerList = (List) selMap.get("LecturerList");
        if (null != lecturerList && !lecturerList.isEmpty()) {
            if (lecturerType.equals("01") || lecturerType.equals("02")) {
                list.add("序号");
                list.add("个人编码");
                list.add("讲师姓名");
                list.add("所属机构");
                list.add("出生日期");
                list.add("年龄");
                list.add("性别");
                list.add("学历");
                list.add("岗位");
                list.add("入司时间");
                list.add("到岗时间");
                list.add("在岗年限");
                list.add("办公电话");
                list.add("手机号码");
                list.add("岗位状态");
                list.add("异动时间");
                list.add("异动说明");
                list.add("离职时间");
                list.add("评聘技术职级");
                list.add("评聘时间");
                list.add("技术职级考核结果");
                list.add("考核时间");
                list.add("获奖情况");
                list.add("擅长课程");
                list.add("其他备注");
                lists.add(list);
                for (int i = 0; i < lecturerList.size(); i++) {
                    Map map = (Map) lecturerList.get(i);
                    List nextList = new ArrayList();
                    nextList.add(map.get("number"));
                    nextList.add(map.get("userCode"));
                    nextList.add(map.get("userName"));
                    nextList.add(ReturnString(map.get("comName")));
                    nextList.add(ReturnString(map.get("bornDate")));
                    nextList.add(ReturnString(map.get("age")));
                    String sex = "";
                    if (!StringUtils.isEmpty(map.get("sex"))) {
                        sex = map.get("sex").equals("0") ? "男" : "女";
                    }
                    nextList.add(sex);
                    Map codeName = (Map) map.get("codeName");
                    nextList.add(ReturnString(codeName.get("codeName")));
                    nextList.add(ReturnString(map.get("JobsName")));
                    nextList.add(ReturnString(map.get("employDate")));
                    nextList.add(ReturnString(map.get("workDate")));
                    nextList.add(ReturnString(map.get("workYear")));
                    nextList.add(ReturnString(map.get("officePhone")));
                    nextList.add(ReturnString(map.get("phone")));
                    nextList.add(ReturnString(map.get("JobsState")));
                    nextList.add(ReturnString(map.get("moveDate")));
                    nextList.add(ReturnString(map.get("moveNote")));
                    nextList.add(ReturnString(map.get("departureDate")));
                    nextList.add(ReturnString(map.get("technologyRank")));
                    nextList.add(ReturnString(map.get("undoubtedlyDate")));
                    nextList.add(ReturnString(map.get("inspectionData")));
                    nextList.add(ReturnString(map.get("inspectionDate")));
                    List winningList = (List) map.get("winningList");
                    String winningS = "";
                    for (int j = 0; j < winningList.size(); j++) {
                        Object o = winningList.get(j);
                        winningS += o;
                        if (j != winningList.size() - 1) {
                            winningS += "_";
                        }
                    }
                    nextList.add(winningS);
                    List courseList = (List) map.get("courseList");
                    String courseS = "";
                    for (int j = 0; j < courseList.size(); j++) {
                        Object o = courseList.get(j);
                        courseS += o;
                        if (j != courseList.size() - 1) {
                            courseS += "_";
                        }
                    }
                    nextList.add(courseS);
                    nextList.add(ReturnString(map.get("otherNote")));
                    lists.add(nextList);
                }
            }
            if (lecturerType.equals("03")) {
                list.add("序号");
                list.add("个人编码");
                list.add("讲师姓名");
                list.add("所属机构");
                list.add("出生日期");
                list.add("年龄");
                list.add("性别");
                list.add("学历");
                list.add("代理人职级");
                list.add("入司时间");
                list.add("入司年限");
                list.add("手机号码");
                list.add("兼讲星级");
                list.add("聘任时间");
                list.add("获奖情况");
                list.add("擅长课程");
                list.add("其他备注");
                lists.add(list);
                for (int i = 0; i < lecturerList.size(); i++) {
                    Map map = (Map) lecturerList.get(i);
                    List nextList = new ArrayList();
                    nextList.add(map.get("number"));
                    nextList.add(map.get("userCode"));
                    nextList.add(map.get("userName"));
                    nextList.add(ReturnString(map.get("comName")));
                    nextList.add(ReturnString(map.get("bornDate")));
                    nextList.add(ReturnString(map.get("age")));
                    String sex = "";
                    if (!StringUtils.isEmpty(map.get("sex"))) {
                        sex = map.get("sex").equals("0") ? "男" : "女";
                    }
                    nextList.add(sex);
                    Map codeName = (Map) map.get("codeName");
                    nextList.add(ReturnString(codeName.get("codeName")));
                    nextList.add(ReturnString(map.get("agentgradeName")));
                    nextList.add(ReturnString(map.get("employDate")));
                    nextList.add(ReturnString(map.get("employYear")));
                    nextList.add(ReturnString(map.get("phone")));
                    nextList.add(ReturnString(map.get("startName")));
                    nextList.add(ReturnString(map.get("AppointmentDate")));
                    List winningList = (List) map.get("winningList");
                    String winningS = "";
                    for (int j = 0; j < winningList.size(); j++) {
                        Object o = winningList.get(j);
                        winningS += o;
                        if (j != winningList.size() - 1) {
                            winningS += "_";
                        }
                    }
                    nextList.add(winningS);
                    List courseList = (List) map.get("courseList");
                    String courseS = "";
                    for (int j = 0; j < courseList.size(); j++) {
                        Object o = courseList.get(j);
                        courseS += o;
                        if (j != courseList.size() - 1) {
                            courseS += "_";
                        }
                    }
                    nextList.add(courseS);
                    nextList.add(ReturnString(map.get("otherNote")));
                    lists.add(nextList);
                }
            }
            if (lecturerType.equals("04")) {
                list.add("序号");
                list.add("个人编码");
                list.add("讲师姓名");
                list.add("所属机构");
                list.add("出生日期");
                list.add("年龄");
                list.add("性别");
                list.add("学历");
                list.add("现任职务");
                list.add("入司时间");
                list.add("入司年限");
                list.add("手机号码");
                lists.add(list);
                for (int i = 0; i < lecturerList.size(); i++) {
                    Map map = (Map) lecturerList.get(i);
                    List nextList = new ArrayList();
                    nextList.add(map.get("number"));
                    nextList.add(map.get("userCode"));
                    nextList.add(map.get("userName"));
                    nextList.add(ReturnString(map.get("comName")));
                    nextList.add(ReturnString(map.get("bornDate")));
                    nextList.add(ReturnString(map.get("age")));
                    String sex = "";
                    if (!StringUtils.isEmpty(map.get("sex"))) {
                        sex = map.get("sex").equals("0") ? "男" : "女";
                    }
                    nextList.add(sex);
                    Map codeName = (Map) map.get("codeName");
                    nextList.add(ReturnString(codeName.get("codeName")));
                    nextList.add(ReturnString(map.get("nowPosition")));
                    nextList.add(ReturnString(map.get("employDate")));
                    nextList.add(ReturnString(map.get("employYear")));
                    nextList.add(ReturnString(map.get("phone")));
                    lists.add(nextList);
                }
            }
            return lists;
        } else {
            return null;
        }
    }

    public Object ReturnString(Object obj) {
        return StringUtils.isEmpty(obj) ? "" : obj;
    }
    public  XSSFWorkbook makeExcel(List<List> data){
        // 创建新的Excel工作簿
        XSSFWorkbook workbook = new XSSFWorkbook();
        // 在Excel工作簿中建一工作表，其名为缺省值, 也可以指定Sheet名称
        XSSFSheet sheet = workbook.createSheet();
        for (int i = 0; i < data.size(); i++) {
            XSSFRow row = sheet.createRow(i);
            List list = data.get(i);
            for (int j = 0; j < list.size(); j++) {
                XSSFCell cell = row.createCell(j);
                cell.setCellValue((String)list.get(j));
            }
        }
        return workbook;
    }

    @Test
    public void test12() throws IOException {

    }




}
